package com.qqbb.gateway.config;


import com.qqbb.gateway.loadbalancer.GrayLoadBalancer;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.loadbalancer.core.ReactorLoadBalancer;
import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier;
import org.springframework.cloud.loadbalancer.support.LoadBalancerClientFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;

/**
 * @author fqq
 */

public class LoadBalancerConfig {


    @Bean
    ReactorLoadBalancer<ServiceInstance> grayLoadBalancer(Environment environment,
                                                          LoadBalancerClientFactory loadBalancerClientFactory) {
        //默认版本
        String defaultVersion = environment.getProperty("qqbb.grayLoadbalancer.defaultVersion");
        //服务名称
        String name = environment.getProperty(LoadBalancerClientFactory.PROPERTY_NAME);
        //灰度均衡
        return new GrayLoadBalancer(loadBalancerClientFactory.getLazyProvider(name, ServiceInstanceListSupplier.class), defaultVersion, name);
    }

}
